package com.shenjun.userver.handler;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class HelloServerHandler extends ChannelInitializer<NioSocketChannel> {


    @Override
    protected void initChannel(NioSocketChannel channel) throws Exception {
        log.info("8002服务器连接建立。。。");
        channel.pipeline().addLast(new LengthFieldBasedFrameDecoder(
                1024, 0, 4,
                0, 4));
        channel.pipeline().addLast(new StringDecoder());
        channel.pipeline().addLast(new SimpleChannelInboundHandler<String>() {
            @Override
            public void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
                log.info("8002服务端收到消息：【{}】", msg);
            }
        });
    }
}
